import 'dart:convert';

import 'package:device_info/device_info.dart';
import 'package:flutter_des/flutter_des.dart';
import 'package:imei_plugin/imei_plugin.dart';
import 'package:package_info/package_info.dart';

import '../public.dart';

class VersionService {
  static Future<void> check() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String version;
    String id;
    String userType;
    var imei =
        await ImeiPlugin.getImei(shouldShowRequestPermissionRationale: true);
    print("IMEI $imei");
    if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      id = iosInfo.identifierForVendor;
      userType = "ios";
      //ios相关代码
    } else if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      version = packageInfo.version;
      id = androidInfo.androidId +
          "|${androidInfo.device}|${androidInfo.brand}|${androidInfo.model}|$imei";
      userType = "android";
      //android相关代码
    }
    version = "&version=$version";
    id = "&id=$id";
    userType = "&userType=$userType";
    var param = await FlutterDes.encryptToHex(
        version + id + userType, AppConfig.cryptKey,
        iv: AppConfig.cryptIv);

    String url = AppConfig.BASE_UEL + Api.checkUpdate + param.toString();
    BaseResp res = await HttpManager().request(url);
    Map<String, dynamic> jsonObject = jsonDecode(res.data);
    if (jsonObject['retCode'] == 1000) {
      Fluttertoast.showToast(msg: jsonObject['msg']);
    } else if (jsonObject['retCode'] == 1001) {
      Fluttertoast.showToast(msg: jsonObject['msg']);
      EventBus().emit("newVersionTips", jsonObject['app']);
    }
  }
}
